package com.tckdq.tcc.mq.listener;

import com.tckdq.tcc.domain.CourseOrder;
import com.tckdq.tcc.service.ICourseOrderService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
@RocketMQTransactionListener(txProducerGroup = "tx_producer_group_courseorder")
public class CourseOrderTransactionListener implements RocketMQLocalTransactionListener {
    //执行本地事物的方法:保存订单和订单明细
    //msg:消息对象
    //arg:扩展参数,作为订单传入进来
    @Autowired
    private ICourseOrderService courseOrderService;
    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object arg) {
        try {
            if (arg !=null && arg instanceof CourseOrder){
                CourseOrder courseOrder = (CourseOrder)arg;
                courseOrderService.saveOrderAndItem(courseOrder);
                return RocketMQLocalTransactionState.COMMIT;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RocketMQLocalTransactionState.ROLLBACK;
    }

    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
        return null;
    }
}
